home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 010 / subs.arc / DOSCMD.ASM next >
Assembly Source File  |  1987-07-28  |  3KB  |  146 lines

  1.  
  2.     name    dos_cmd
  3.  
  4.  
  5. save    macro    p1,p2,p3,p4,p5,p6,p7,p8,p9
  6.     irp    y,<p1,p2,p3,p4,p5,p6,p7,p8,p9>
  7.     ifnb    <y>
  8.     push    y
  9.     endif
  10.     endm
  11. restore macro
  12.     irp    z,<p9,p8,p7,p6,p5,p4,p3,p2,p1>
  13.     ifnb    <z>
  14.     pop    z
  15.     endif
  16.     endm
  17.     endm
  18.     endm
  19.  
  20.  
  21. dseg    segment    byte    public    'data'
  22.  
  23. comspec    db    'COMSPEC',0
  24.  
  25. dseg    ends
  26.  
  27.  
  28. cseg    segment    byte    public    'code'
  29.  
  30.  
  31.     public    dos_command
  32.  
  33.     comment    `
  34.  
  35. Routine to execute any DOS command.
  36.  
  37. Calling stack:
  38.     <sp+00> -> flag word -
  39.             0    execute COMMAND.COM, using the command line
  40.             1    load a copy of COMMAND.COM, no command line
  41.                 present.
  42.     (if sp+00 = 0)
  43.     <sp+02> -> dword pointer to command line (ASCIZ string)
  44.  
  45. Returns:
  46.     AX contains return code from command.
  47.  
  48. Notes:
  49.     Requires getenv to get the COMSPEC environment variable, and
  50.     run_program to execute COMMAND.COM.
  51.  
  52. `
  53.  
  54.     assume    cs:cseg,ds:dseg
  55.  
  56.     extrn    getenv:near
  57.     extrn    run_program:near
  58.  
  59.  
  60. dos_command    proc    near
  61.  
  62. CommandFlag    equ    word ptr [bp+4]
  63. DosCmdLine    equ    dword ptr [bp+6]
  64.     push    bp            ; Save incoming fp
  65.     mov    bp,sp            ; Get a new fp
  66.     save    bx,si,di,ds,es        ; Save registers
  67.     mov    bx,bp            ; Save our current fp
  68.     sub    sp,76            ; Room for COMSPEC value
  69.     mov    bp,sp            ; Pointer to it
  70.     mov    byte ptr [bp],76    ; Set length of buffer
  71.     push    ss            ; Set up parameters for getenv
  72.     push    bp
  73.     mov    ax,dseg            ; Get the data seg we need
  74.     push    ax
  75.     mov    ax,offset comspec    ; Pointer to the string
  76.     push    ax
  77.     call    getenv            ; Get COMSPEC value
  78.     xchg    bp,bx            ; Get our fp back
  79.     cmp    CommandFlag,0        ; Need to use the command line?
  80.     je    UseCommandLine        ; Yes, so do it
  81.     sub    sp,2            ; Need 2 bytes on the stack
  82.     mov    di,sp            ; Get pointer to them
  83.     push    ss
  84.     pop    es
  85.     mov    ax,0d00h        ; Put 2 bytes onto stack
  86.     stosw
  87.     sub    di,2            ; Point to them again
  88.     push    es            ; Put pointer on stack
  89.     push    di
  90.     jmp    short StackFNPointer    ; Continue
  91. UseCommandLine:
  92.     lds    si,DosCmdLine        ; Get pointer to command line
  93.     sub    sp,80h            ; Allow room for command line
  94.     mov    di,sp            ; Get pointer to area
  95.     push    ss            ; Need the segment
  96.     pop    es
  97.     push    bx            ; Save pointer to file name
  98.     mov    bx,di            ; Point to length byte
  99.     inc    di            ; Point to buffer area
  100.     mov    byte ptr ss:[bx],3    ; Set length byte
  101.     mov    al,'/'            ; Put the switch into the command line
  102.     stosb
  103.     mov    al,'C'
  104.     stosb
  105.     mov    al,' '
  106.     stosb
  107.     mov    cx,123            ; Max length we'll use
  108. StoreCmdLine:
  109.     lodsb                ; Get a byte of the command line
  110.     or    al,al            ; Is it the terminator?
  111.     jz    NoMoreCmdLine        ; Yep. No more command line
  112.     stosb                ; Put that baby away
  113.     inc    byte ptr ss:[bx]    ; Increment line length
  114.     loop    StoreCmdLine        ; Loop for max or until terminator
  115. NoMoreCmdLine:
  116.     mov    al,0dh            ; Put in terminating <cr>
  117.     stosb
  118.     pop    bx            ; Get pointer to file name back
  119.     mov    ax,sp            ; Get the pointer to command line
  120.     push    ss            ; Put pointer on the stack
  121.     push    ax            ;  (dword pointer)
  122. StackFNPointer:
  123.     push    ss
  124.     inc    bx
  125.     push    bx            ; Put pointer to file name on stack
  126.     call    run_program        ; Run the program
  127.     sub    bp,10
  128.     mov    sp,bp            ; Cut the stack back
  129.     restore                ; Get registers back
  130.     add    bp,10
  131.     cmp    CommandFlag,0        ; Did we use a command line?
  132.     jnz    PopLess            ; No, so pop fewer bytes
  133.     pop    bp            ; Restore fp
  134.     ret    6            ; Return to caller
  135. ;
  136. PopLess:
  137.     pop    bp            ; Restore fp
  138.     ret    2            ; Return with smaller stack cutback
  139.  
  140. dos_command    endp
  141.  
  142.  
  143. cseg    ends
  144.  
  145.     end
  146.